package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.common.R;
import com.itheima.common.ThreadCommon;
import com.itheima.pojo.ShoppingCart;
import com.itheima.service.DishService;
import com.itheima.service.SetmealService;
import com.itheima.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    ShoppingCartService shoppingCartService;

    @Autowired
    DishService dishService;

    @Autowired
    SetmealService setmealService;


    //用户购物车添加菜品
    /*@PostMapping("/add")
    public R add(@RequestBody ShoppingCart shoppingCart) {
        //获取用户id
        Long logic = ThreadCommon.getTableLogic();
        //获取id在购物车系统中的数据对象
        QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId());
        queryWrapper.lambda().eq(shoppingCart.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        //queryWrapper.lambda().eq(shoppingCart.getDishFlavor()!=null,ShoppingCart::getDishFlavor,shoppingCart.getDishFlavor());
        queryWrapper.lambda().eq(ShoppingCart::getUserId, logic);
        //获取到的用户的购物车对象
        ShoppingCart serviceOne = shoppingCartService.getOne(queryWrapper);
        shoppingCart.setUserId(logic);
        //判断前端传入的是菜品还是套餐
        if (shoppingCart.getDishId() != null) {
            //传入的是菜品
            log.info("传入菜品执行了");
            QueryWrapper<Dish> wrapper = new QueryWrapper<>();
            wrapper.lambda().eq(Dish::getId, shoppingCart.getDishId());
            Dish one = dishService.getOne(wrapper);
            //shoppingCart.setId(one.getId());
            shoppingCart.setAmount(one.getPrice());
            //以上已完成数据的修改
            //判断用户在购物车表中是否有对象
            if (serviceOne == null) {
                //不等于null表示 用户没有购物车对象 创建用户购物车对象
                log.info("没有购物车 创建购物车执行了");
                shoppingCart.setNumber(1);
                shoppingCartService.save(shoppingCart);
            } else {
                log.info("有购物车 修改购物车执行了");
                serviceOne.setNumber(serviceOne.getNumber() + 1);
                shoppingCartService.updateById(serviceOne);
            }
        } else if (shoppingCart.getSetmealId() != null) {
            //走到这里说明用户选择的是套餐
            //获取套餐信息对象
            QueryWrapper<Setmeal> wrapper = new QueryWrapper<>();
            wrapper.lambda().eq(Setmeal::getId, shoppingCart.getSetmealId());
            Setmeal one = setmealService.getOne(wrapper);

            shoppingCart.setAmount(one.getPrice());

            if (serviceOne == null) {
                shoppingCart.setNumber(1);
                shoppingCartService.save(shoppingCart);
            } else {
                serviceOne.setNumber(serviceOne.getNumber() + 1);
                shoppingCartService.updateById(serviceOne);
            }
        } else {
            throw new RepeatException("添加购物车出现未知错误");
        }
        return R.success("添加成功");
    }*/
    @PostMapping("/add")
    public R add(@RequestBody ShoppingCart shoppingCart) {
        //获取用户id
        Long logic = ThreadCommon.getTableLogic();
        //获取id在购物车系统中的数据对象
        QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ShoppingCart::getUserId, logic);
        Long dishId = shoppingCart.getDishId();
        if(dishId!=null){
            //传入的是菜品
            queryWrapper.lambda().eq(ShoppingCart::getDishId,dishId);
        }else {
            queryWrapper.lambda().eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        //获取购物车数据信息
        ShoppingCart one = shoppingCartService.getOne(queryWrapper);
        if(one==null){
            //说明数据库中还没有该用户的信息
            shoppingCart.setNumber(1);
            shoppingCart.setUserId(logic);
            shoppingCartService.save(shoppingCart);
        }else {
            //不为null 则更新数据
            one.setNumber(one.getNumber()+1);
            shoppingCartService.updateById(one);
        }
        return R.success("添加成功");
    }

    //查询购物车
    @GetMapping("/list")
    public R getAll() {
        List<ShoppingCart> list = shoppingCartService.list();
        return R.success(list);
    }

    //取消购物车
    @PostMapping("/sub")
    public R sub(@RequestBody ShoppingCart shoppingCart){
        if(shoppingCart.getDishId()!=null){
            QueryWrapper<ShoppingCart> wrapper = new QueryWrapper<>();
            wrapper.lambda().eq(ShoppingCart::getDishId,shoppingCart.getDishId());
            ShoppingCart one = shoppingCartService.getOne(wrapper);
            if(one.getNumber()>1){
                one.setNumber(one.getNumber()-1);
                shoppingCartService.updateById(one);
            }else {
                shoppingCartService.removeById(one.getId());
            }

        }else {
            QueryWrapper<ShoppingCart> wrapper = new QueryWrapper<>();
            wrapper.lambda().eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
            ShoppingCart one = shoppingCartService.getOne(wrapper);
            if(one.getNumber()>1){
                one.setNumber(one.getNumber()-1);
                shoppingCartService.updateById(one);
            }else {
                shoppingCartService.removeById(one.getId());
            }
        }
        return R.success("取消成功");
    }
        //清空购物车
    @DeleteMapping("/clean")
    public R clean(){
        Long tableLogic = ThreadCommon.getTableLogic();
        QueryWrapper<ShoppingCart> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(ShoppingCart::getUserId,tableLogic);
        shoppingCartService.remove(wrapper);
        return R.success("清空购物车成功");
    }

}
